﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CommonLib
{
    public class IOTools
    {
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="from">文件源位置</param>
        /// <param name="to">复制到的文件位置</param>
        /// <returns></returns>
        public static bool CopyFilePlus(string from, string to,out string newFilePath)
        {
            bool flag = false;
            string fileShortName = System.IO.Path.GetFileName(from);
            string filePath = System.IO.Path.GetDirectoryName(from);
            string filename = to + "\\" + fileShortName;
            try
            {
                if (System.IO.File.Exists(filename))
                {
                    DialogResult dd = MessageBox.Show("文件“" + filename + "”已经存在，\n点击“是”将覆盖原有文件，\n点击“否”将重命名选中文件然后复制,\n点击“取消”将会取消复制命令。", "是否覆盖已存在的文件", MessageBoxButtons.YesNoCancel);
                    if (dd == DialogResult.Yes)
                    {
                        System.IO.File.Copy(from, filename, true);
                        flag = true;
                    }
                    else if (dd == DialogResult.No)
                    {

                        int i = 1;
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(filename);
                        string tb = System.IO.Path.GetExtension(filename);
                        while (System.IO.File.Exists(to + "\\" + fileName + "(" + i.ToString() + ")" + tb))
                        {
                            i++;
                        }
                        filename = to + "\\" + fileName + "(" + i.ToString() + ")" + tb;
                        System.IO.File.Copy(from, filename, true);
                        flag = true;
                    }
                    else
                    {
                        newFilePath = "";
                        return false;
                    }
                }
                else
                {
                    System.IO.File.Copy(from, filename, true);
                    flag = true;
                }
            }
            catch (Exception exc) { MessageBox.Show(exc.Message); }
            newFilePath = filename;
            return flag;
        }

        /// <summary>
        ///移动文件
        /// </summary>
        /// <param name="from">文件源位置</param>
        /// <param name="to">移动到的文件位置</param>
        /// <returns></returns>
        public static bool MoveFilePlus(string from, string to)
        {
            bool flag = false;
            try
            {
                string filename = to;
                if (System.IO.File.Exists(filename))
                {
                    DialogResult dd = MessageBox.Show("文件" + filename + "已经存在，\n点击“是”将覆盖原有文件，\n点击“否”将重命名选中文件然后移动,\n点击“取消”将会取消移动命令。", "是否覆盖已存在的文件", MessageBoxButtons.YesNoCancel);
                    if (dd == DialogResult.Yes)
                    {
                        System.IO.File.Delete(filename);
                        System.IO.File.Move(from, filename);
                        flag = true;
                    }
                    else if (dd == DialogResult.No)
                    {
                        int i = 0;
                        string ta = System.IO.Path.GetFileNameWithoutExtension(filename);
                        string tb = System.IO.Path.GetExtension(filename);//eg: ".jpg"
                        while (System.IO.File.Exists(ta + "(" + i.ToString() + ")" + tb)) i++;
                        filename = ta + "(" + i.ToString() + ")" + tb;
                        System.IO.File.Move(from, filename);
                        flag = true;
                    }
                    else return flag;
                }
                else
                {
                    System.IO.File.Move(from, filename);
                    flag = true;
                }
            }
            catch (Exception exc) { MessageBox.Show(exc.Message); }
            return flag;
        }

        public static bool CopyshpFileToWorkSapce(string from, string to,out string newFileName)
        {
            bool flag = false;
            string newFile = "";
            try
            {
                bool isExist=false ;
                string fileShortName = System.IO.Path.GetFileNameWithoutExtension(from);//获取shapefile的名称
                string filePath = System.IO.Path.GetDirectoryName(from);//获取导出shapefile的路径
                foreach (FileInfo file in new DirectoryInfo(to).GetFiles())
                {
                    if (file.Name==fileShortName+".shp")
                    {
                        isExist = true;
                        DialogResult dr = MessageBox.Show("文件 “" + file.Name + "”已经存在，\n点击“是”将覆盖原有文件，\n点击“否”将重命名选中文件然后复制,\n点击“取消”将会取消复制命令。", "是否覆盖已存在的文件", MessageBoxButtons.YesNoCancel);
                        if (dr == DialogResult.Yes)
                        {
                            string extensionName = System.IO.Path.GetExtension(from);//eg: ".shp"
                            DirectoryInfo shpDi = new DirectoryInfo(filePath);
                            foreach (FileInfo fi in shpDi.GetFiles())
                            {
                                if (fi.Name.Contains(Path.GetFileNameWithoutExtension(fileShortName)))
                                {
                                    string fiPath = filePath + "\\" + fi.Name;
                                    string toFileName = to + "\\" + Path.GetFileNameWithoutExtension(from) + fi.Extension;
                                    System.IO.File.Copy(fiPath, toFileName, true);
                                    flag = true;
                                }
                            }
                            newFile = to + "\\" + Path.GetFileNameWithoutExtension(from) + ".shp";
                        }
                        else if (dr == DialogResult.No)//重命名
                        {
                            int i = 1;
                            string extensionName = System.IO.Path.GetExtension(from);//eg: ".shp"
                            while (System.IO.File.Exists(to+"\\"+fileShortName + "(" + i.ToString() + ")" + extensionName))
                            {
                                i++;
                            }
                            DirectoryInfo shpDi = new DirectoryInfo(filePath);
                            foreach (FileInfo fi in shpDi.GetFiles())
                            {
                                if (fi.Name.Contains(Path.GetFileNameWithoutExtension(fileShortName)))
                                {
                                    string fiPath = filePath + "\\" + fi.Name;
                                    string toFileName = to + "\\" + Path.GetFileNameWithoutExtension(from) + "(" + i.ToString() + ")" + fi.Extension;
                                    System.IO.File.Copy(fiPath, toFileName, false);
                                    flag = true;
                                }
                            }
                            newFile = to + "\\" + Path.GetFileNameWithoutExtension(from) + "(" + i.ToString() + ")" + ".shp";
                        }
                        else 
                        {
                            newFileName="";
                            return flag ;
                        }
                    }
                }
                if (!isExist)//直接复制
                {
                    string extensionName = System.IO.Path.GetExtension(from);//eg: ".shp"
                    DirectoryInfo shpDi = new DirectoryInfo(filePath);
                    foreach (FileInfo fi in shpDi.GetFiles())
                    {
                        if (fi.Name.Contains(Path.GetFileNameWithoutExtension(fileShortName)))
                        {
                            string fiPath = filePath + "\\" + fi.Name;
                            string toFileName = to + "\\" + Path.GetFileNameWithoutExtension(from) + fi.Extension;
                            System.IO.File.Copy(fiPath, toFileName, false);
                            flag = true;
                        }
                    }
                    newFile = to + "\\" + Path.GetFileNameWithoutExtension(from) + ".shp";
                }
                isExist = true;
            }
            catch (Exception exc) { MessageBox.Show(exc.Message); }
            newFileName = newFile;
            return flag;
        }

        /// <summary>求两个文件名的集合的并集</summary>
        public static List<string> UnionFileNameSet(string[] fileList1, string[] fileList2)
        {
            List<string> d = new List<string>();
            for (int i = 0; i < fileList1.Count(); i++)
                d.Add(fileList1[i].Remove(fileList1[i].LastIndexOf(".")));
            bool test = false;
            for (int i = 0; i < fileList2.Count(); i++)
            {
                test = true;
                string a = fileList2[i].Remove(fileList2[i].LastIndexOf("."));
                for (int j = 0; j < fileList1.Count(); j++)
                {
                    string b = fileList1[j].Remove(fileList1[j].LastIndexOf("."));
                    if (a.Equals(b)) { test = false; break; }
                }
                if (test) d.Add(a);
            }
            return d;
        }
    }
}
